package com.example.demo.service.impl;

import com.example.demo.dao.QueryDao;
import com.example.demo.domain.*;
import com.example.demo.exception.BaseException;
import com.example.demo.service.QueryService;
import com.example.demo.util.FileUtil;
import com.example.demo.util.JsonUtil;
import com.example.demo.util.SQLUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class QueryServiceImpl implements QueryService {

    @Autowired
    private QueryDao queryDao;

    private static final Logger logger = LoggerFactory.getLogger(QueryServiceImpl.class);

    private static String projectPath = FileUtil.projectPath;

    private static String sepa = FileUtil.getSystemSeparator();

    public QueryConfig getQueryConfig(String projectName, String queryName, String fillName) {
        try {
            String configFilePath = projectPath + sepa + "query_config" + sepa + projectName + sepa +
                    queryName + sepa + fillName + ".json";
            String jsonStr = JsonUtil.readFromFile(configFilePath);
            return JsonUtil.parseObject(jsonStr, QueryConfig.class);
        } catch (Exception e) {
            throw e;
        }

    }

    @Override
    public List<Map> executeSql(String projectName, String queryName, Map<String, Object> conditions, String fillName) {
        try {
            QueryConfig queryConfig = getQueryConfig(projectName, queryName, fillName);
//            String sql = queryConfig.getSql().toUpperCase(); 20240903去除大写转换
            String sql = queryConfig.getSql();
            sql = SQLUtil.setConditions(sql, conditions);
            return queryDao.executeSql(sql);
        } catch (Exception e) {
            throw new BaseException(e);
        }

    }

    @Override
    public List<Map> executeSqlTotalCount(String projectName, String queryName, Map<String, Object> conditions, String fillName) {
        try {
            QueryConfig queryConfig = getQueryConfig(projectName, queryName, fillName);
            String sqltotalcount = queryConfig.getSqltotalcount().toUpperCase();
            logger.info(sqltotalcount);
            sqltotalcount = SQLUtil.setConditions(sqltotalcount, conditions);
            return queryDao.executeSql(sqltotalcount);
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }


    @Override
    public List<Condition> getConditions(String projectName, String queryName, Map<String, Object> conditionsSql, String fillName) {
        try {
            QueryConfig queryConfig = getQueryConfig(projectName, queryName, fillName);
            //处理列表选项初始化，如果sql不为空，则把SQL执行结果做为选项初始化
            //SQL返回结果需要与CombItem一致ID、ITEMCODE,ITEMNAME
            for (Condition c : queryConfig.getConditions()) {
                if (!StringUtils.isEmpty(c.getSql())) {
                    String sql = c.getSql();
                    sql = SQLUtil.setConditions(sql, conditionsSql);
                    List<Map> itemList = queryDao.executeSql(sql);
                    List<CombItem> combItems = new ArrayList<>();
                    for (Map item : itemList) {
                        combItems.add(JsonUtil.mapToObject(item, CombItem.class));
                    }
                    c.setInit(combItems);
                }

                if (!StringUtils.isEmpty(c.getSqlDate())) {
                    String sqlDate = c.getSqlDate();
                    sqlDate = SQLUtil.setConditions(sqlDate, conditionsSql);
                    List<Map> itemList = queryDao.executeSql(sqlDate);
                    List<CombDate> combDates = new ArrayList<>();
                    for (Map item : itemList) {
                        combDates.add(JsonUtil.mapToObject(item, CombDate.class));
                    }
                    c.setInitDate(combDates);
                }
            }
            return queryConfig.getConditions();
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }

    @Override
    public List<TableField> getTableFields(String projectName, String queryName, String fillName) {
        try {
            QueryConfig queryConfig = getQueryConfig(projectName, queryName, fillName);
            return queryConfig.getTableFields();
        } catch (Exception e) {
            throw new BaseException(e);
        }
    }
}
